package com.jin;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import com.jin.domain.CheckItem;
import com.jin.domain.ChooseItem;

public class Xuanzeti extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    private static Xuanzeti frame;
    private static Xuanzeti frameSecond;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    List<ChooseItem> list = ItemSource.chooseItems;
                    int index = new Random().nextInt(list.size());
                    // ChooseItem chooseItemFirst = list.get(index);
                    frame = new Xuanzeti(index);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Xuanzeti(int index) {
        ChooseItem chooseItemFirst = ItemSource.chooseItems.get(index);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 373);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel label = new JLabel("选择题");
        label.setBounds(173, 10, 54, 15);
        contentPane.add(label);
        int indexItem=ExamResult.getIndexAndIncrement();
        JLabel label_1 = new JLabel("第" + indexItem + "题");
        label_1.setBounds(10, 37, 54, 15);
        contentPane.add(label_1);

        JTextArea panduanTitle01 = new JTextArea(chooseItemFirst.getItemTitle());
        panduanTitle01.setLineWrap(true);
        panduanTitle01.setBounds(10, 62, 550, 45);
        contentPane.add(panduanTitle01);

        JLabel lbla = new JLabel("A:" + chooseItemFirst.getChooseA());
        lbla.setBounds(10, 117, 300, 15);
        contentPane.add(lbla);

        JLabel lblb = new JLabel("B:" + chooseItemFirst.getChooseB());
        lblb.setBounds(10, 157, 300, 15);
        contentPane.add(lblb);

        JLabel lblc = new JLabel("C:" + chooseItemFirst.getChooseC());
        lblc.setBounds(10, 203, 300, 15);
        contentPane.add(lblc);

        JLabel lbld = new JLabel("D:" + chooseItemFirst.getChooseD());
        lbld.setBounds(10, 248, 300, 15);
        contentPane.add(lbld);

        JButton button = new JButton("下一题");
        button.setBounds(352, 279, 93, 23);
        contentPane.add(button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String choose = textField.getText();
                if (choose == null || "".equals(choose) || choose.trim().length() == 0) {
                    JOptionPane.showMessageDialog(frameSecond == null ? frame : frameSecond, "必须作答",
                            "标题", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                String result = textField.getText().toLowerCase();
                String answer = chooseItemFirst.getAnswer().toLowerCase();
                int index = indexItem;
                if (result.equals(answer)) {
                    ExamResult.add("第" + index + "题:正确");
                } else {
                    ExamResult.add("第" + index + "题:错误");
                    ExamResult.add("正确答案:" + chooseItemFirst.getAnswer());
                }
                frame.dispose();
                if (ExamResult.getIndex() == 3) {
                    // 调转到判断页面
                    List<CheckItem> list = ItemSource.checkItems;
                    int indexCheck = new Random().nextInt(list.size());
                    Panduanti panduan1 = new Panduanti(indexCheck);
                    panduan1.setVisible(true);
                    ExamResult.setCurFrame(panduan1);
                    frameSecond.dispose();
                } else {
                    List<ChooseItem> list = ItemSource.chooseItems;
                    // ChooseItem chooseItemFirst = list.get(1);
                    int indexNew = index + 1;
                    if (indexNew >= list.size()) {
                        indexNew = 0;
                    }
                    frameSecond = new Xuanzeti(indexNew);
                    frameSecond.setVisible(true);
                }

            }
        });

        textField = new JTextField();
        textField.setBounds(130, 280, 66, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_2 = new JLabel("答案");
        label_2.setBounds(76, 283, 34, 15);
        contentPane.add(label_2);
    }
}
